package com.jspgou.cms.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.CustomerServiceDao;
import com.jspgou.cms.entity.CustomerService;

@Repository
public class CustomerServiceDaoImpl extends HibernateBaseDao<CustomerService, Long> 
    implements CustomerServiceDao{

	public CustomerService findById(Long id) {
		CustomerService entity = get(id);
		return entity;
	}
	
	public Pagination getPagination(Boolean disable,int pageNo, int pageSize){
		Finder f = Finder.create("from CustomerService bean where bean.store.id is null");
		if(disable!=null){
			f.append(" and bean.disable=:disable").setParam("disable", disable);
		}
		f.append(" order by bean.priority");
		return find(f, pageNo, pageSize);
	}

	public Pagination getPagination(Long webId,Long storeId,Integer orderby,
			Boolean disable,int pageNo, int pageSize){
		Finder f = Finder.create("from CustomerService bean where 1=1");
		if(storeId!=null){
			f.append(" and bean.website.id=:webId").setParam("webId", webId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId").setParam("storeId", storeId);
		}
		if(disable!=null){
			f.append(" and bean.disable=:disable").setParam("disable", disable);
		}
		if(orderby==null){
			orderby=1;
		}
		appendOrder(f,orderby);
		return find(f, pageNo, pageSize);
	}
	
	@SuppressWarnings("unchecked")
	public List<CustomerService> getList(Long webId,Long storeId,Integer orderby,Boolean disable){
		Finder f = Finder.create("from CustomerService bean where 1=1");
		if(storeId!=null){
			f.append(" and bean.website.id=:webId").setParam("webId", webId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId").setParam("storeId", storeId);
		}else{
			f.append(" and bean.store.id is null");
		}
		if(disable!=null){
			f.append(" and bean.disable=:disable").setParam("disable", disable);
		}
		if(orderby==null){
			orderby=1;
		}
		appendOrder(f,orderby);
		return find(f);
	}

	public CustomerService save(CustomerService bean) {
		getSession().save(bean);
		return bean;
	}
	
	public CustomerService deleteById(Long id) {
		CustomerService entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	private void appendOrder(Finder f, int orderBy) {
		switch (orderBy) {
		case 1:
			// ID升序
			f.append(" order by bean.id asc");
			break;
		case 2:
			// 创建时间降序
			f.append(" order by bean.priority asc");
			break;
		default:
			// 默认： ID降序
			f.append(" order by bean.id desc");
		}
	}

	@Override
	protected Class<CustomerService> getEntityClass() {
		return CustomerService.class;
	}

}

